package com.sun.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sun.dao.mapper.ArticleMapper;
import com.sun.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ThreadService {


    //希望此线程在线程池中执行，不可以影响主线程
    @Async("taskExecutor")
    public void updateArticleViewCounts(ArticleMapper articleMapper, Article article) {
        Integer viewCounts = article.getViewCounts();
        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(viewCounts+1);
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getId,article.getAuthorId());
        //为了在多线程环境下 线程安全 需要需要做一个操作
        wrapper.eq(Article::getViewCounts,viewCounts);
        articleMapper.update(articleUpdate,wrapper);
        try {
            Thread.sleep(5000);
            System.out.println("更新完成了");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
